#Dennis Moskov, Master Thesis
#clustering promoter, base metals,support and preparation methods into catalyst groups
#clustering by Hierarchical Clustering
#use "fpc" and "cluster" packages
     
#install.packages("fpc")
#install.packages("cluster")
#library(fpc)
#library(cluster)

#chose variables for clustering
DBc<-DB[2:17]

#create dissimilarity object
clus<-daisy(DBc, metric = "gower")    #mixed variables

#apply hierarchical clustering  
hClus <- hclust(dissM, method="complete")  #dissM=gower, dissRF=random forest

#plot dendogram
x11()
plot(hClus,type ='l', labels = FALSE,xlab="Data Points",main="Cluster Dendogram\nMethod: Complete ; Input: Dissimilarity Matrix (Gower)",sub="",ylim=c(0, 1.2*max(hClus$height)))

#safe dendogram
png(filename="Dendogram.png")
plot(hClus,type ='l', labels = FALSE,xlab="Data Points",main="Cluster Dendogram\nMethod: Complete ; Input: Dissimilarity Matrix (Gower)",sub="",ylim=c(0, 1.2*max(hClus$height)))
dev.off()

#choosen cluster
nkdendo<-6    #by dendogram height 

#cluster accordingly
hClusdendo <- cutree(hClus, k=nkdendo) 

#plot
x11()
plot(hClusdendo,xlab="Data Points",main=c("Cluster Distribution by Dendogram Height\n",nkdendo," Cluster"),ylab="Cluster",ylim=c(1,nkdendo))

#safe to file
png(filename="ClusterDendo.png")
plot(hClusdendo,xlab="Data Points",main=c("Cluster Distribution by Dendogram Height\n",nkdendo," Cluster"),ylab="Cluster",ylim=c(1,nkdendo))
dev.off()


write.csv(hClusdendo,file="clustersDendo.csv")

#add clusters to data base
DBc<-cbind(DB[1],hClusdendo,DB[-(1:17)])
DBc$hClusdendo<-as.factor(DBc$hClusdendo)
